package com.jbl.ma.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jbl.commonutils.R;
import com.jbl.ma.entity.*;
import com.jbl.ma.service.RepairDescribeService;
import com.jbl.ma.service.RepairService;
import com.jbl.ma.vo.ApplyRejectVO;
import com.jbl.ma.vo.RepairQuery;
import com.jbl.ma.vo.RepairVo;
import com.jbl.ma.vo.UserApplyVo;
import com.jbl.te.entity.Teacher;
import com.jbl.te.service.TeacherService;
import com.jbl.te.service.UserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jbl
 * @since 2020-12-11
 */

@CrossOrigin
@RestController
@RequestMapping("/ma/repair")
public class RepairController {
    @Autowired
    private RepairService repairService;
    @Autowired
    private TeacherService teacherService;

    @PostMapping("/saveOrUpdateRepair")
    @ApiOperation(value = "创建更新维修")
    public R saveOrUpdateRepair(@RequestBody RepairVo vo) {
        String id = vo.getRepairId();
        //id为空，或者已接收和已维修，创建
        if(StringUtils.isEmpty(id) || (!StringUtils.isEmpty(vo.getUserId()) && vo.getRepairStatus() == 1)) {
            Repair repair = new Repair();
            repair.setRepairType(vo.getRepairType());
            repair.setTeacherNumber(vo.getTeacherNumber());
            repair.setContent(vo.getContent());
            boolean save = repairService.save(repair);
            if(!save) return R.error().message("添加维修失败！");
        } else {
            Repair repair = new Repair();
            BeanUtils.copyProperties(vo,repair);
            boolean update = repairService.updateById(repair);
            if(!update) return R.error().message("更新申请失败！");
        }
        return R.ok();
    }

    @ApiOperation(value = "通过用户id获取最新维修")
    @GetMapping("/getRepairByUserId")
    public R getRepairByUserId(String id) {
        //获取最新repair
        RepairVo vo = new RepairVo();
        QueryWrapper<Repair> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_number",id);
        wrapper.orderByDesc("gmt_create");
        wrapper.last("limit 1");
        List<Repair> repairList = repairService.list(wrapper);
        Repair repair = null;
        if(!repairList.isEmpty()) repair = repairList.get(0);

        //最新repair存在，赋值给vo
        if(repair != null) {
            BeanUtils.copyProperties(repair,vo);
            if(repair.getUserId() != null) {
                Teacher teacher = teacherService.getById(repair.getUserId());
                if(teacher != null) {
                    vo.setUserName(teacher.getName());
                    vo.setUserPhone(teacher.getPhone());
                }
            }
        }

        return R.ok().data("repair",vo);
    }




    @GetMapping("/repair/{repairId}")
    @ApiOperation("处理维修")
    public R repair(@PathVariable String repairId) {
        boolean update = repairService.repair(repairId);
        if (update) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @PostMapping("/repairbatch")
    @ApiOperation("批量处理维修")
    public R repairBatch(@RequestBody List<String> repairIds) {
        List<String> collect = repairService.repairBatch(repairIds);
        if (collect.size() > 0) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @GetMapping("/receive/{repairId}/{userId}")
    @ApiOperation("管理员接收维修")
    public R receive(@PathVariable String repairId,
                     @PathVariable String userId) {
        boolean update = repairService.receive(repairId, userId);
        if (update) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @PostMapping("/receivebatch/{userId}")
    @ApiOperation("批量接收维修")
    public R receiveBatch(@RequestBody List<String> repairIds,
                          @PathVariable String userId) {
        List<String> collect = repairService.receiveBatch(repairIds, userId);
        if (collect.size() > 0) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation("获取分页查询维修列表")
    @PostMapping("list/{page}/{limit}")
    public R pageQuery(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "repairQuery", value = "查询对象", required = false)
            @RequestBody(required = false) RepairQuery repairQuery) {

        Page<Repair> pageParam = new Page<>(page, limit);
        List<RepairVo> collect = repairService.pageQuery(pageParam, repairQuery);
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("dataList", collect);
    }

    @GetMapping("/list")
    @ApiOperation("获取维修列表")
    public R list() {
        List<RepairVo> collect = repairService.repairList();
        return R.ok().data("dataList", collect);
    }

    @PostMapping("/add")
    @ApiOperation("添加维修")
    public R add(RepairVo vo) {
        Repair repair = new Repair();
        repair.setTeacherNumber(vo.getTeacherNumber());
        repair.setRepairType(vo.getRepairType());
        repair.setRepairStatus(0);
        repair.setContent(vo.getContent());
        repairService.save(repair);

        return R.ok();
    }
}
